﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FsCarInfo.Core.Entity.IRespositories;
using HMIS.FrameWork.Repository.Nhb;
using FsCarInfo.Core.Entity;
using HMIS.FrameWork.DomainBase;
using HMIS.FrameWork.Repository;

namespace FsCarInfo.Core.Entity.IRespositories.Impl
{
	public class CarTransferRepositoryImpl : RepositoryNhbImpl<CarTransfer>, ICarTransferRepository
	{
		#region ICarRepository 成员

		public IList<CarTransfer> GetPlistAll(int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
		{
			var strFilter = base.GetHqlstrByExtFilter(filters, "c");
			sort = "c." + sort;

			var query = Session.CreateQuery(@"select c from Car as c"
					+ (string.IsNullOrEmpty(strFilter) ? string.Empty : " where " + strFilter)
					+ " order by " + sort + " " + dir)
					.SetFirstResult(start)
					.SetMaxResults(limit);

			total = Session.CreateQuery(@"select count(*) from Car as c "
				 + (string.IsNullOrEmpty(strFilter) ? string.Empty : " where " + strFilter))
				.UniqueResult<long>();

			return query.List<CarTransfer>();
		}

		public bool IsFieldExist(string fieldName, string fieldValue, string id)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, null);
		}

		/// <summary>
		/// 查询调拨单列表
		/// </summary>
		/// <param name="query"></param>
		/// <param name="start"></param>
		/// <param name="limit"></param>
		/// <param name="sort"></param>
		/// <param name="dir"></param>
		/// <param name="filters"></param>
		/// <param name="total"></param>
		/// <returns></returns>
		public IList<CarTransfer> QueryPlist(string query, 
			int start, 
			int limit, 
			string sort,
			string dir,
			List<DataFilter> filters, out long total)
		{
			// filter
			var strFilter = base.GetHqlstrByExtFilter(filters, "t");
			// 排序
			string ordStr = string.IsNullOrEmpty(sort) ? string.Empty : " order by t." + sort + " " + dir;
			// Sql条件
			string sqlStr = @" from CarTransfer as t "
				+ (string.IsNullOrEmpty(strFilter) ? string.Empty : " where " + strFilter);

			// 查询
			var q = Session.CreateQuery(@"select t " + sqlStr + ordStr);
				//.SetString("query", query + "%");

			// 汇总
			var q_total = Session.CreateQuery(@"select count(*) " + sqlStr);
				//.SetString("query", query + "%");

			// 设置结果集
			if (limit > 0)
				q.SetFirstResult(start).SetMaxResults(limit);

			// 返回
			total = q_total.UniqueResult<long>();
			return q.List<CarTransfer>();
		}

		/// <summary>
		/// 扩展下。具备按query查询和filter筛选功能
		/// </summary>
		/// <param name="query"></param>
		/// <param name="start"></param>
		/// <param name="limit"></param>
		/// <param name="sort"></param>
		/// <param name="dir"></param>
		/// <param name="filters"></param>
		/// <param name="total"></param>
		/// <returns></returns>
		public IList<CarTransfer> QueryCarList(string query, int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
		{
			// filter
			var strFilter = base.GetHqlstrByExtFilter(filters, "c");
			// 排序
			string ordStr = string.IsNullOrEmpty(sort) ? string.Empty : " order by c." + sort + " " + dir;
			// sql条件
			string sqlStr = @"from Car as c 
								where (lower(c.IDCardNo) like lower(:query)
									or lower(c.Engin_ID) like lower(:query) 
									or lower(c.Frame_ID) like lower(:query))"
				+ (string.IsNullOrEmpty(strFilter) ? string.Empty : " and " + strFilter);


			var q = Session.CreateQuery(@"select c " + sqlStr + ordStr)
						.SetString("query", "%" + query + "%");
			if (limit > 0)
				q.SetFirstResult(start).SetMaxResults(limit);

			total = Session.CreateQuery(@"select count(*) " + sqlStr)
				.SetString("query", "%" + query + "%")
				.UniqueResult<long>();

			return q.List<CarTransfer>();
		}

		#endregion
	}
}
